<?php
//yubing@baixing.com

class SearchResult implements IteratorAggregate {
	protected $totalCount = 0, $ids = [];
  protected $class;
  public $error;

	public function __construct($ids = [], $totalCount = -1) {
		$this->ids = $ids;
		$this->totalCount = $totalCount;
	}

	public function ids() {
		return $this->ids;
	}

	public function objs() {
    $class = $this->class;
		return $class::load($this->ids);
	}

	public function count() {
		return count($this->ids);
	}

	public function totalCount() {
		return $this->totalCount;
	}

	public function getIterator() {
		return new ArrayIterator($this->objs());
	}

  public function data() {
    return [
      'ids' => $this->ids,
      'totalCount' => $this->totalCount,
      'class' => $this->class,
    ];
  }

  public static function loadFromData($data) {
    $res = new SearchResult();
    $res->ids = $data['ids'];
    $res->totalCount = $data['totalCount'];
    $res->class = $data['class'];
    return $res;
  }
}
